//
//  XYOriginalView.swift
//  01-新浪微博
//
//  Created by 陈许亚 on 16/3/12.
//  Copyright © 2016年 cxy. All rights reserved.
//
//原创微博View


//pic_urls 数组  存储字典---key "thumbnail_pic"

 //单张图片直接显示原始尺寸，两张及两张以上按固定尺寸进行显示

import UIKit
import SnapKit

//全局变量，一般声明在类的最上面
//定义一个全局的间距
let statusCellMargin: CGFloat = 10

class XYOriginalView: UIView {
    
    var status: XYStatusesModel? {
        didSet{
          //绑定模型数据
            //头像
            iconView.sd_setImageWithURL(status?.user?.headImgURL)
            //vip等级
            verifiedView.image = status?.user?.verified_type_image
            //微博名称
            nameLabel.text = status?.user?.name
            //会员等级
            mbrankView.image = status?.user?.mbrank_image
            
            //TODO:以后解决时间和来源
            //时间  Fri Mar 11 22:01:50 +0800 2016
            timeLable.text  = status?.created_at
            //来源 
            //"<a href=\"http://app.weibo.com/t/feed/1sxHP2\" rel=\"nofollow\">专业版微博</a>"
            if let s = status?.sourceText {
                
                sourceLable.text = "来自 " + s
            }
            
            //print(status?.source)
            
            //微博正文
            contentLable.text = status?.text
            
            pictureView.imageURLs = status?.imageURLs
            
        }
    }

    
    
    //UIView默认的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子视图布局
    private func setUpUI(){
        
        //设置原创微博背景色
//        backgroundColor = UIColor.redColor()
          backgroundColor = UIColor.whiteColor()
        addSubview(sepView)
        addSubview(iconView)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(mbrankView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(contentLable)
        addSubview(pictureView)
        
        //分割线
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(statusCellMargin)
        }
        
        sepView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            //TODO:约束是否正确
            make.top.left.equalTo(self).offset(statusCellMargin)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        iconView.layer.cornerRadius = 20
        iconView.clipsToBounds = true
        //iconView.layer.masksToBounds = true
        //vip标识
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        //名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
            
        }
        //用户等级
        mbrankView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
        }
        
        //时间标签
        timeLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_bottom).offset(statusCellMargin / 2)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
        }
        //来源标签
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLable.snp_top)
            make.left.equalTo(timeLable.snp_right).offset(statusCellMargin)
        }
        
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(iconView.snp_left)
            //对label设置右边距，加上line是= 0可以换行
            make.right.equalTo(self.snp_right).offset(-statusCellMargin)
            
            //make.height.equalTo(<#T##other: CGFloat##CGFloat#>)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLable.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentLable.snp_left)
            //不需要添加，用宽度来解决
//            make.right.equalTo(self.snp_right).offset(-statusCellMargin)
            //设置测试大小
//            /make.size.equalTo(CGSize(width: 100, height: 100))
//            make.bottom.equalTo(self.snp_bottom)
        }
        pictureView.backgroundColor = UIColor.redColor()
        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
        }
        
    }
    
    
    //MARK:懒加载子视图
        //分隔线
        private var sepView: UIView = UIView()
    //头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    
    //用户类型图标（verified_type）（VIP ）
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级mbrank（0--6）
    private lazy var mbrankView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //用户名
    private lazy var nameLabel: UILabel = UIFactory.labelFactory("123", textColor: UIColor.blackColor(), fontSize: 17)
    //更新时间
    private lazy var timeLable: UILabel = UIFactory.labelFactory("10:01", textColor: UIColor.darkGrayColor(), fontSize: 12)
    
    //微博来源
    private lazy var sourceLable: UILabel = UIFactory.labelFactory("手机", textColor: UIColor.darkGrayColor(), fontSize: 12)
    //    //微博正文
    private lazy var contentLable: UILabel = UIFactory.labelFactory("你是我的眼", textColor: UIColor.blackColor(), fontSize: 17,alignment: .Left)
    
    //配图视图
    private lazy var pictureView: XYPictureView = XYPictureView()
    
}
